<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<script type="text/javascript" src="dist/xlsx.core.min.js"></script>
<script type="text/javascript" src="../js/jquery-3.5.1.js"></script>
<style type="text/css">
    table {
        border-collapse: collapse;
    }

    th, td {
        border: solid 1px #6D6D6D;
        padding: 5px 10px;
    }

    .mt-sm {
        margin-top: 8px;
    }

    body {
        background: #f4f4f4;
        padding: 0;
        margin: 0;
    }

    .container {
        width: 1024px;
        margin: 0 auto;
        background: #fff;
        padding: 20px;
        min-height: 100vh;
    }
</style>
<body>
<div id="result" contenteditable="">
    <table>
        <tbody>
        <tr>
            <th></th>
            <th>A</th>
            <th>B</th>
            <th>C</th>
            <th>D</th>
        </tr>
        <tr>
            <td>1</td>
            <td>姓名</td>
            <td>性别</td>
            <td>年龄</td>
            <td>籍贯</td>
        </tr>
        <tr>
            <td>2</td>
            <td>张三</td>
            <td>男</td>
            <td>18</td>
            <td>广东惠州</td>
        </tr>
        <tr>
            <td>3</td>
            <td>李四</td>
            <td>女</td>
            <td>22</td>
            <td>河北石家庄</td>
        </tr>
        <tr>
            <td>4</td>
            <td>王二麻子</td>
            <td>人妖</td>
            <td>19</td>
            <td>泰国曼谷</td>
        </tr>
        </tbody>
    </table>
</div>
<h2>导出excel</h2>

<div class="mt-sm" style="padding-bottom:40px;">
    <input type="button" onclick="outPutExcel()" value="保存"/>
    上面读取的表格您可以直接编辑，编辑后点击保存即可导出excel文件。
</div>
</body>
<script>

    var set1 = [];
    $('table tr').each(function () {
        var row = [];
        $(this).find('th').each(function () {
            row.push($(this).text());
        });
        $(this).find('td').each(function () {
            row.push($(this).text());
        });
        set1.push(row)
    });

    console.log(set1)
    function outPutExcel() {
        var sheet = XLSX.utils.aoa_to_sheet(set1);
        openDownloadDialog(sheet2blob(sheet), '导出.xlsx');
    }
    // 将一个sheet转成最终的excel文件的blob对象，然后利用URL.createObjectURL下载
    function sheet2blob(sheet, sheetName) {
        sheetName = sheetName || 'sheet1';
        var workbook = {
            SheetNames: [sheetName],
            Sheets: {}
        };
        workbook.Sheets[sheetName] = sheet;
        // 生成excel的配置项
        var wopts = {
            bookType: 'xlsx', // 要生成的文件类型
            bookSST: false, // 是否生成Shared String Table，官方解释是，如果开启生成速度会下降，但在低版本IOS设备上有更好的兼容性
            type: 'binary'
        };
        var wbout = XLSX.write(workbook, wopts);
        var blob = new Blob([s2ab(wbout)], {type:"application/octet-stream"});
        // 字符串转ArrayBuffer
        function s2ab(s) {
            var buf = new ArrayBuffer(s.length);
            var view = new Uint8Array(buf);
            for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
            return buf;
        }
        return blob;
    }


    /**
     * 通用的打开下载对话框方法，没有测试过具体兼容性
     * @param url 下载地址，也可以是一个blob对象，必选
     * @param saveName 保存文件名，可选
     */
    function openDownloadDialog(url, saveName) {
        if (typeof url == 'object' && url instanceof Blob) {
            url = URL.createObjectURL(url); // 创建blob地址
        }
        var aLink = document.createElement('a');
        aLink.href = url;
        aLink.download = saveName || ''; // HTML5新增的属性，指定保存文件名，可以不要后缀，注意，file:///模式下不会生效
        var event;
        if (window.MouseEvent) event = new MouseEvent('click');
        else {
            event = document.createEvent('MouseEvents');
            event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
        }
        aLink.dispatchEvent(event);
    }

    $(function () {
        document.getElementById('file').addEventListener('change', function (e) {
            var files = e.target.files;
            if (files.length == 0) return;
            var f = files[0];
            if (!/\.xlsx$/g.test(f.name)) {
                alert('仅支持读取xlsx格式！');
                return;
            }
            readWorkbookFromLocalFile(f, function (workbook) {
                readWorkbook(workbook);
            });
        });
        loadRemoteFile('./sample/test.xlsx');
    });

    function loadRemoteFile(url) {
        readWorkbookFromRemoteFile(url, function (workbook) {
            readWorkbook(workbook);
        });
    }



</script>
</html>